import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  UseGuards,
  Req,
} from '@nestjs/common';
import { DrawService } from './draw.service';
import { CreateDrawDto } from './dto/create-draw.dto';
import { UpdateDrawDto } from './dto/update-draw.dto';
import { AuthGuard } from '@nestjs/passport';
import { ToolsService } from 'src/utils/tools.service';
import { DrawEntity } from './entities/draw.entity';
import { ApiTags } from '@nestjs/swagger';


@ApiTags('绘画模块')  
@Controller('draw')
export class DrawController {
  constructor(private readonly drawService: DrawService) {}

  @Post('/add')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  create(@Body() createDrawDto: CreateDrawDto, @Req() req) {
    try {
      return this.drawService.create(createDrawDto, req.user.userid);
    } catch (error) {
      ToolsService.fail(error);
    }
  }

  @Post('/addUseSql')
  createUseNativeSql(@Body() createDrawDto: CreateDrawDto) {
    try {
      return this.drawService.createUseNativeSql(createDrawDto);
    } catch (error) {
      ToolsService.fail(error);
    }
  }


  @Get('/list')
  findAll(
    @Query('pageSize') pageSize: number,
    @Query('currentPage') currentPage: number,
    @Query('title') title: string,
    @Query('userId') userId: string,
  ) {
    return this.drawService.findAll(
      title,
      {
        pageSize: pageSize ? pageSize : 100,
        currentPage: currentPage ? currentPage - 1 : 0,
      },
      userId,
    );
  }

  @Get(':id')
  findOne(@Param('id') id: number) {
    let  draw =   this.drawService.findOne(id);
    return draw;
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateDrawDto: UpdateDrawDto) {
    try {
      return this.drawService.update(id, updateDrawDto);
    } catch (error) {
      ToolsService.fail(error);
    }
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    try {
      return this.drawService.remove(id);
    } catch (error) {
      ToolsService.fail(error);
    }
  }
}
